package com.wxn.learncompose.pagers

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.ClickableText
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog

@Composable
fun ClickableTextPager() {

    var showProtocol by remember {
        mutableStateOf(false)
    }
    if (showProtocol) {
        Dialog(onDismissRequest = {
            showProtocol = false
        }) {
            Card(
                modifier = Modifier
                    .shadow(5.dp, RoundedCornerShape(6.dp))
                    .background(Color.White)
            ) {

                Column(
                    modifier = Modifier
                        .padding(12.dp, 18.dp)
                        .size(280.dp, 160.dp),
                    verticalArrangement = Arrangement.Top,
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    Text(text = "用户协议",
                        fontSize = TextUnit(16f, TextUnitType.Sp),
                        fontWeight = FontWeight.Bold,
                        color = Color(0xFF553355)
                    )
                    Text(text = "用户协议内容如下xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
                        fontSize = TextUnit(14f, TextUnitType.Sp),
                        fontWeight = FontWeight.Normal,
                        color = Color(0xFF333333))
                    Row (
                        verticalAlignment = Alignment.CenterVertically,
                        horizontalArrangement = Arrangement.End
                    ) {
                        Button(onClick = { showProtocol = false }) {
                            Text(text = "取消")
                        }
                        Button(onClick = { showProtocol = false }) {
                            Text(text = "同意")
                        }

                    }

                }
            }
        }
    }

    Surface {
        Column(
            verticalArrangement = Arrangement.Top,
            horizontalAlignment = Alignment.Start,
            modifier = Modifier.padding(12.dp, 24.dp)

        ) {

            ClickableText(text = buildAnnotatedString {
                    append("勾选即表示同意")
                    withStyle(SpanStyle(Color.Blue, fontWeight = FontWeight.Bold)) {
                        append("用户协议")
                    }
                },
                onClick = { idx->
                    Log.i("aaa", "$idx clicked")
                    if (idx >= 7) {
                        showProtocol = true
                    }
                })

        }
    }
}